package com.nian.servlet;

import com.nian.pojo.User;
import com.nian.service.UserService;
import com.nian.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/client/user.do")
public class UserServlet extends BaseServlet {
    //1.登录 ?action=login
    public void login(HttpServletRequest req, HttpServletResponse resp){
        UserService userService = new UserServiceImpl();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User login = userService.login(user);
        if (login!=null){
            try {
                req.getRequestDispatcher("../index.html").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    //2.注册  ?action=register
    public void register(HttpServletRequest req, HttpServletResponse resp){
        UserService userService = new UserServiceImpl();
        String username = req.getParameter("username");
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        String telephone = req.getParameter("telephone");
        String gender = req.getParameter("gender");
        String introduce = req.getParameter("introduce");

        User user = new User();
        user.setUsername(username);
        user.setEmail(email);
        user.setPassword(password);
        user.setTelephone(telephone);
        user.setGender(gender);
        user.setIntroduce(introduce);
        boolean result  = userService.isRegister(user);
        if (result){
            try {
                req.getRequestDispatcher("./login.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
